package com.yikaiye.android.yikaiye.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.utils.DateUtil;

import java.util.Calendar;

public abstract class TimePicker {

    private TimePickerView pickerView;
    private TextView tvTitle;

    public TimePicker(Context context, boolean isDialog) {
        Calendar calendar = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.add(Calendar.YEAR, -6);
        Calendar endDate = Calendar.getInstance();
        endDate.add(Calendar.YEAR, 0);
        pickerView = new TimePickerBuilder(context, (selDate, v) -> {
            pickerView.dismiss();
            confirm(DateUtil.dateToString(selDate));
        }).setLayoutRes(R.layout.pk_date_item, v -> {
                    //自定义布局中的控件初始化及事件处理
                    final TextView tvSubmit = v.findViewById(R.id.tv_finish);
                    final TextView tv_cancel = v.findViewById(R.id.tv_cancel);
                    tvTitle = v.findViewById(R.id.tv_title);
                    tvSubmit.setOnClickListener(v1 -> pickerView.returnData());
                    tv_cancel.setOnClickListener(v12 -> pickerView.dismiss());
                }).setType(new boolean[]{true, true, false, false, false, false})// 默认全部显示
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                .setBgColor(Color.WHITE)//滚轮背景颜色 Night mode
                .setDate(calendar)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(isDialog)//是否显示为对话框样式
                .build();
        if (isDialog) {
            Dialog mDialog = pickerView.getDialog();
            if (mDialog != null) {
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) pickerView.getDialogContainerLayout().getLayoutParams();
                params.leftMargin = 0;
                params.rightMargin = 0;
                pickerView.getDialogContainerLayout().setLayoutParams(params);
                Window dialogWindow = mDialog.getWindow();
                if (dialogWindow != null) {
                    dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION);
                    WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
                    attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                    attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    attributes.windowAnimations = com.bigkoo.pickerview.R.style.picker_view_slide_anim; // 修改动画样式
                    attributes.gravity = Gravity.BOTTOM;
                    dialogWindow.setAttributes(attributes);
                }
            }
        }
    }

    public abstract void confirm(String date);

    public void showPickerView() {
        pickerView.show();
    }

    public void setPickerViewTitle(String title) {
        tvTitle.setText(title);
    }
}
